
#include "gps_proto_util.h"

#include "kml/base/math_util.h"

namespace gps_proto {

int ComputeBounds(const GpsProto& gps_proto, BoundsProto* bounds_proto) {
  int num_pts = 0;
  bounds_proto->set_maxlat(-90);
  bounds_proto->set_minlat(90);
  bounds_proto->set_maxlon(-180);
  bounds_proto->set_minlon(180);
  for (int t = 0; t < gps_proto.track_size(); ++t) {
    for (int s = 0; s < gps_proto.track(t).segment_size(); ++s) {
      num_pts += ComputeSegmentBounds(gps_proto.track(t).segment(s),
                                      bounds_proto);
    }
  }
  return num_pts;
}

int ComputeSegmentBounds(const SegmentProto& segment,
                         BoundsProto* bounds_proto) {
  int num_pts = 0;
  for (int p = 0; p < segment.point_size(); ++p) {
    const PointProto& point = segment.point(p);
    ++num_pts;
    if (point.lat() > bounds_proto->maxlat()) {
      bounds_proto->set_maxlat(point.lat());
    }
    if (point.lat() < bounds_proto->minlat()) {
      bounds_proto->set_minlat(point.lat());
    }
    if (point.lon() > bounds_proto->maxlon()) {
      bounds_proto->set_maxlon(point.lon());
    }
    if (point.lon() < bounds_proto->minlon()) {
      bounds_proto->set_minlon(point.lon());
    }
  }
  return num_pts;
}

double TotalSegmentDistance(const SegmentProto& segment, double* ascent,
                            double* descent) {
  double dist = 0;
  for (int p = 1; p < segment.point_size(); ++p) {
    // As in moving from point A to point B.
    const PointProto& a = segment.point(p-1);
    const PointProto& b = segment.point(p);
    dist += kmlbase::DistanceBetweenPoints3d(
                a.lat(), a.lon(), a.ele(),
                b.lat(), b.lon(), b.ele());
    if (ascent && b.ele() > a.ele()) {
      *ascent += b.ele() - a.ele();
    } else if (descent && b.ele() < a.ele()) {
      *descent += b.ele() - a.ele();
    }
  }
  return dist;
}

int ComputeBounds(const GpsProto& gps_proto,
                  const BoundsProto& bounds_proto) {
}

}  // end namespace gpb

#define GPB_UTIL_H__
